﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Net;
using System.Threading;
using Windows.Data.Json;
using System.Net.Http;
using System.Threading.Tasks;
                                                                                                                                                                                                                
using W8IIG.DataModel;
using W8IIG.Common;

namespace W8IIG.DataController
{
    class DataResource
    {
        /* url to fetch collections list */
        private string _url;

        private ObservableCollection<Collection> _collection_list;
        public ObservableCollection<Collection> CollectionList
        {
            get
            {
                return this._collection_list;
            }
        }

        public DataResource(string url)
        {
            this._url = url;
            this._collection_list = new ObservableCollection<Collection>();
        }

        /*
         * get all data about collections, boards
         * */
        public async void fetchData()
        {
            HttpClient mHttpClient = new HttpClient();
            Uri resource;
            if (!Uri.TryCreate(_url, UriKind.Absolute, out resource))
            {
                return;
            }
            try
            {
                HttpResponseMessage hrm = await mHttpClient.GetAsync(resource);
                string content = await hrm.Content.ReadAsStringAsync();
                parseDataToCollectionsList(content);
            }
            catch (ArgumentNullException) { }
        }

        private async void parseDataToCollectionsList(string data_str)
        {
            JsonArray collection_list = JsonArray.Parse(data_str);
            for (uint i = 0; i < collection_list.Count; i++)
            {
                JsonObject collection = collection_list.GetObjectAt(i);
                string collectionId = collection.GetNamedString(Contains.COLLECTION_ID_KEY);
                string collectionName = collection.GetNamedString(Contains.COLLECTION_NAME_KEY);
                string thumbnail = Contains.SERVER_ADDRESS + "/collections/" + collectionId + "/thumbnail";
                
                Collection mCollection = new Collection(collectionId, collectionName, thumbnail);
                
                //mCollection.Boards = await getBoardsList(collectionId);

                this._collection_list.Add(mCollection);
            }
        }

        private async Task< ObservableCollection<Board> > getBoardsList(string collectionId)
        {
            ObservableCollection<Board> result = new ObservableCollection<Board>();

            // url to get boards list of specified collection
            string url = this._url + "/" + collectionId;
            Uri resource;
            if (!Uri.TryCreate(url, UriKind.Absolute, out resource))
            {
                return null;
            }

            HttpClient mHttpClient = new HttpClient();
            HttpResponseMessage mHttpResponseMessage = await mHttpClient.GetAsync(resource);
            string boards_list_str = await mHttpResponseMessage.Content.ReadAsStringAsync();

            /* Due to server side response data is currently wrong, this code block will be disabled
            
            JsonArray boars_list = JsonArray.Parse(boards_list_str);
            for (uint i = 0; i < boars_list.Count; i++)
            {
                JsonObject board = boars_list.GetObjectAt(i);
                string boardId = board.GetNamedString(Contains.BOARD_ID_KEY);
                string boardName = board.GetNamedString(Contains.BOARD_NAME_KEY);
                string thumbnail = Contains.SERVER_ADDRESS + "/boards/" + boardId + "/thumbnail";

                result.Add(new Board(boardId, boardName, thumbnail, collectionId));
            }

            return result;
            
            */

            return null;

        }
    }
}
